package cn.tedu.v3_2;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Version 3_2 聊天室登录功能实现(自写版本)
 */
public class Server {
    private ServerSocket serverSocket;
    private static final Integer PORT=6666;
    //1.私有化属性:Map--userDatabase
    private static final Map<String,String> userDatabase=new HashMap<>();

    public Server(){
       //2.创建两个账号和密码
            userDatabase.put("空桑少主","12345");
            userDatabase.put("鹄羹","45678");
        try {
            serverSocket=new ServerSocket(PORT);
        } catch (BindException e){
            System.out.println("端口号重复申请，建议换个端口号");
        } catch (IOException e) {
            System.out.println("申请端口号失败...");
        }
    }

    public void start()  {
        try {
            while (true){
            System.out.println("-------等待客户端链接------");
            Socket socket = serverSocket.accept();

            new Thread(new ClientHandler(socket)).start();
            }
        } catch (IOException e) {
            System.out.println("服务器端通讯断开");
        } finally {
            if (serverSocket!=null&&!serverSocket.isClosed()){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败");
                }
            }
        }
    }

    public static void main(String[] args)  {
        new Server().start();
    }
    //5.1 使用私有属性在主方法外编写Login登录验证
    private boolean Login(String name,String password){
        
        return true;
    }
    //5.2 编写handChat登录聊天功能
    public void handleChat(){}
    private class ClientHandler implements Runnable{
        private Socket socket;
        private String ip;
        private BufferedReader br;
        private PrintWriter pw;
        //3.创建username、password私有属性
        private String username;
        private String password;
        public ClientHandler(Socket s){
            this.socket=s;
            ip=socket.getInetAddress().getHostAddress();
            try {
                 br=new BufferedReader(new InputStreamReader
                        (socket.getInputStream(),"UTF8"));
                 pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter
                        (socket.getOutputStream(),
                                "UTF8")), true);
            } catch (IOException e) {
                System.out.println("客户端断开通讯~");
            }
        }

        public void run() {
            try {
                //4.进行登录验证
                pw.println("请输入用户名: ");
                username= br.readLine();
                pw.println("请输入密码: ");
                password= br.readLine();
            String str=null;
             new Thread(()->{
                 try{
                 String message;
                 while ((message=br.readLine())!=null){
                     System.out.println("客户端说: "+message);
                     System.out.println("服务器端请输入: ");
                 }
                 }catch (IOException e){
                     System.out.println("客户端读入失败!");
                 }
             }).start();
               Scanner input=new Scanner(System.in);
                while(true){
                    System.out.println(ip+"服务器请输入: ");
                    String say=input.nextLine();

                    pw.println("服务器说: "+say);
                }
            } catch (NullPointerException e) {
                System.out.println("IO流传输对象，有对象为null,请立马核对。"
                        +"建议: 查看try块中对象可能赋值为null");
            } catch (IOException e){
                System.out.println("服务器断开链接");
            }finally {
                //输入流关闭
                if (br!=null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        System.out.println("通讯关闭失败~");
                    }
                }
                //输出流关闭
                if (pw!=null) pw.close();
                if (socket!=null&&!socket.isClosed()){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        System.out.println("通讯关闭失败~");
                    }
                }
            }
        }
    }
}
